package com.ymatou.iapi.productmanager.testcase;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.productmanager.service.SaveCatalogPackageListCall;
import com.ymatou.iapi.productmanager.service.UpdateProductSensitiveWordCall;
import com.ymatou.iapi.sellerproduct.parameter.IncludeSensitiveWordBean;
import com.ymatou.iapi.sellerproduct.parameter.ProdTplBean;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.NewJavaMongoCheckService;
import com.ymttest.business.service.SellerProductCallService;
import com.ymttest.database.sqlwapper.YmtCatalogsIWapper;
import com.ymttest.database.sqlwapper.YmtProductDescExtraWapper;
import com.ymttest.database.sqlwapper.YmtProductsIWapper;
import com.ymttest.database.sqlwapper.YmtSubCatalogsWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.JsonBeanHelper;
import com.ymttest.utils.MapUtil;

public class Ts_UpdateProductSensitiveWord {
	private static UpdateProductSensitiveWordCall updateProductSensitiveWordCall = new UpdateProductSensitiveWordCall();
	private static Integer sellerId = Integer.valueOf(EnvSetup.getData("productsellerid"));
	private static String sellerName = EnvSetup.getData("productsellername");	
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("敏感词修改");
	}
	
	@Before
	public void caseUp() {
		
	}
	
	@AfterClass
	public static void tearDown() {
		Logger.generateResult(updateProductSensitiveWordCall.getOpurl());
	}
	
	@After
	public void caseDown() {
		Logger.end();
	}
	
	//待操作的表和字段的枚举值（1- 商品名称，2- 商品文描文字部分，3- 规格名称，4-套装名称， 5-买家须知，6-买手介绍）
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UpdateProductSensitiveWord_001() {
		Logger.start(true,"敏感词打码");
		try{
			IncludeSensitiveWordBean includeSensitiveWordBean = new IncludeSensitiveWordBean();
			
			String sensitiveWord = "毛主席";
			
			
			String productId = addProductWithWord(sellerId,sellerName,sensitiveWord);
			
			//1,2,3,4,5都包含
			checkSensitiveWord(sellerId,productId,sensitiveWord,includeSensitiveWordBean);
			
			//1不包含
			Logger.verifyEquals(true, updateProductSensitiveWordCall.call(productId, sellerId, 1, sensitiveWord),"updateProductSensitiveWord 返回值检查");
			includeSensitiveWordBean.setYmtProducts(false);
			checkSensitiveWord(sellerId,productId,sensitiveWord,includeSensitiveWordBean);
			
			//1,2不包含
			Logger.verifyEquals(true, updateProductSensitiveWordCall.call(productId, sellerId, 2, sensitiveWord),"updateProductSensitiveWord 返回值检查");
			includeSensitiveWordBean.setYmtProductDescConfigWord(false);
			checkSensitiveWord(sellerId,productId,sensitiveWord,includeSensitiveWordBean);
			
			//1,2,3不包含
			Logger.verifyEquals(true, updateProductSensitiveWordCall.call(productId, sellerId, 3, sensitiveWord),"updateProductSensitiveWord 返回值检查");
			includeSensitiveWordBean.setYmtCatalogSetAttributes(false);
			checkSensitiveWord(sellerId,productId,sensitiveWord,includeSensitiveWordBean);
			
			//1,2,3,4不包含
			Logger.verifyEquals(true, updateProductSensitiveWordCall.call(productId, sellerId, 4, sensitiveWord),"updateProductSensitiveWord 返回值检查");
			includeSensitiveWordBean.setYmtSubCatalogs(false);
			checkSensitiveWord(sellerId,productId,sensitiveWord,includeSensitiveWordBean);
			
			
			//1,2,3,4,5不包含
			Logger.verifyEquals(true, updateProductSensitiveWordCall.call(productId, sellerId, 5, sensitiveWord),"updateProductSensitiveWord 返回值检查");
			includeSensitiveWordBean.setYmtProductDescExtra(false);
			checkSensitiveWord(sellerId,productId,sensitiveWord,includeSensitiveWordBean);
			
			
			NewJavaMongoCheckService.checkProductMgoSyncBySQL_NewJavaMgo(productId);
			
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UpdateProductSensitiveWord_002() {
		Logger.start(true,"敏感词打码");
		try{
			IncludeSensitiveWordBean includeSensitiveWordBean = new IncludeSensitiveWordBean();
			
			String sensitiveWord = "fyftest";
			String productId = addProductWithWord(sellerId,sellerName,sensitiveWord);
			
			//1,2,3,4,5都包含
			checkSensitiveWord(sellerId,productId,sensitiveWord,includeSensitiveWordBean);
			
			//名称不包含敏感词
			Logger.verifyEquals(true, updateProductSensitiveWordCall.call(productId, sellerId, 1, "你好"),"updateProductSensitiveWord 返回值检查");
			checkSensitiveWord(sellerId,productId,sensitiveWord,includeSensitiveWordBean);

			//用户id不是该商品
			Logger.verifyEquals(false, updateProductSensitiveWordCall.call(productId, 312320, 1, sensitiveWord),"updateProductSensitiveWord 返回值检查");
			checkSensitiveWord(sellerId,productId,sensitiveWord,includeSensitiveWordBean);
			
			//1不包含
			Logger.verifyEquals(true, updateProductSensitiveWordCall.call(productId, sellerId, 1, sensitiveWord),"updateProductSensitiveWord 返回值检查");
			includeSensitiveWordBean.setYmtProducts(false);
			checkSensitiveWord(sellerId,productId,sensitiveWord,includeSensitiveWordBean);
			
			//1,2不包含
			Logger.verifyEquals(true, updateProductSensitiveWordCall.call(productId, sellerId, 2, sensitiveWord),"updateProductSensitiveWord 返回值检查");
			includeSensitiveWordBean.setYmtProductDescConfigWord(false);
			checkSensitiveWord(sellerId,productId,sensitiveWord,includeSensitiveWordBean);
			
			//1,2,3不包含
			Logger.verifyEquals(true, updateProductSensitiveWordCall.call(productId, sellerId, 3, sensitiveWord),"updateProductSensitiveWord 返回值检查");
			includeSensitiveWordBean.setYmtCatalogSetAttributes(false);
			checkSensitiveWord(sellerId,productId,sensitiveWord,includeSensitiveWordBean);
			
			//1,2,3,4不包含
			Logger.verifyEquals(true, updateProductSensitiveWordCall.call(productId, sellerId, 4, sensitiveWord),"updateProductSensitiveWord 返回值检查");
			includeSensitiveWordBean.setYmtSubCatalogs(false);
			checkSensitiveWord(sellerId,productId,sensitiveWord,includeSensitiveWordBean);
			
			
			//1,2,3,4,5不包含
			Logger.verifyEquals(true, updateProductSensitiveWordCall.call(productId, sellerId, 6, sensitiveWord),"updateProductSensitiveWord 返回值检查");
			includeSensitiveWordBean.setYmtProductDescExtra(false);
			checkSensitiveWord(sellerId,productId,sensitiveWord,includeSensitiveWordBean);
			
			
			NewJavaMongoCheckService.checkProductMgoSyncBySQL_NewJavaMgo(productId);
			
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UpdateProductSensitiveWord_003() {
		Logger.start(true,"敏感词打码,非该商品的买手");
		try{
			IncludeSensitiveWordBean includeSensitiveWordBean = new IncludeSensitiveWordBean();
			
			String sensitiveWord = "fyftest";
			String productId = addProductWithWord(sellerId,sellerName,sensitiveWord);
			
			//1,2,3,4,5都包含
			checkSensitiveWord(sellerId,productId,sensitiveWord,includeSensitiveWordBean);
			
			//名称不包含敏感词
			Logger.verifyEquals(true, updateProductSensitiveWordCall.call(productId, sellerId, 1, "你好"),"updateProductSensitiveWord 返回值检查");
			checkSensitiveWord(sellerId,productId,sensitiveWord,includeSensitiveWordBean);

			//用户id不是该商品
			for (int i=1;i<5 ;i++){
				Logger.verifyEquals(false, updateProductSensitiveWordCall.call(productId, 312320, i, sensitiveWord),"updateProductSensitiveWord 返回值检查");
				checkSensitiveWord(sellerId,productId,sensitiveWord,includeSensitiveWordBean);
			}
			
			
			Logger.verifyEquals(true, updateProductSensitiveWordCall.call("ddddd", sellerId, 5, sensitiveWord),"updateProductSensitiveWord 返回值检查");
			includeSensitiveWordBean.setYmtProductDescExtra(false);
			checkSensitiveWord(sellerId,productId,sensitiveWord,includeSensitiveWordBean);
			
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static void checkSensitiveWord(int sellerId,String productId,String sensitiveWord,IncludeSensitiveWordBean includeSensitiveWordBean) throws Exception{
		YmtProductsIWapper ymtProductsIWapper = new YmtProductsIWapper();
		YmtCatalogsIWapper ymtCatalogsIWapper = new YmtCatalogsIWapper();
		YmtSubCatalogsWapper ymtSubCatalogsWapper = new YmtSubCatalogsWapper();
		YmtProductDescExtraWapper ymtProductDescExtraWapper = new YmtProductDescExtraWapper();
		
		//1
		Map<String,Object> ret1 = ymtProductsIWapper.selectProductByProductIdforMap(productId).get(0);
		Logger.verifyEquals(includeSensitiveWordBean.isYmtProducts(), ret1.get("sProduct").toString().contains(sensitiveWord),"敏感词检查-YmtProducts");
		//2
		Map<String,Object> ret2 = ymtProductsIWapper.selectDescConfigByProductId(productId).get(0);
		Logger.verifyEquals(includeSensitiveWordBean.isYmtProductDescConfigWord(), ret2.get("WordContent").toString().contains(sensitiveWord),"敏感词检查-YmtProductDescConfigWord");
		
		
		List<String> catalogsList = ymtCatalogsIWapper.selectCatalogsByProductId(productId).stream().map(ele->ele.getsCatalogId()).collect(Collectors.toList());

		//3
		boolean isAttrInclude = false;
		for(int i=0;i<catalogsList.size();i++){
			Map<String,Object> ret3 = ymtCatalogsIWapper.selectCatalogAttributeByCatalogId(catalogsList.get(i)).get(0);
			if (ret3.get("sCategoryAttribute").toString().contains(sensitiveWord)){
				isAttrInclude = true;break;
			}
		}
		Logger.verifyEquals(includeSensitiveWordBean.isYmtCatalogSetAttributes(), isAttrInclude, "敏感词检查-YmtCatalogSetAttributes");
		
		//4
		boolean isSubCatalogName = false;
		for(int i=0;i<catalogsList.size();i++){
			String subCataLogName =ymtSubCatalogsWapper.selectByCatalogId(catalogsList.get(i)).getSubCatalogName();
			if (subCataLogName.contains(sensitiveWord)){
				isSubCatalogName = true;break;
			}
		}
		Logger.verifyEquals(includeSensitiveWordBean.isYmtSubCatalogs(), isSubCatalogName, "敏感词检查-YmtSubCatalogs");
		
		
		//5
		boolean isProductDescExtra = false;
		List<Map> ret6 = ymtProductDescExtraWapper.selectBySellerId(sellerId);
		for(int i=0;i<ret6.size();i++){
			if(ret6.get(i).get("Description").toString().contains(sensitiveWord)){
				isProductDescExtra = true;break;
			}
		}
		Logger.verifyEquals(includeSensitiveWordBean.isYmtProductDescExtra(), isProductDescExtra, "敏感词检查-YmtProductDescExtra");
	}
	
	
	
	
	
	
	public static String addProductWithWord(int sellerId,String sellerName,String wordStr) throws Exception{
		
		SellerProductCallService.deleteTemplate(sellerId);
		//	模版类型（1，尺码模版 2，买家须知 3，买手服务）
		ProdTplBean sizeTemplate = SellerProductCallService.saveTemplate(sellerId, 1,MapUtil.hashMap("Description", wordStr+"size模板描述"+wordStr));
		ProdTplBean buyerNoticeTemplate = SellerProductCallService.saveTemplate(sellerId, 2,MapUtil.hashMap("Description", wordStr+"notice模板描述"+wordStr));
		ProdTplBean sellerServiceTemplate = SellerProductCallService.saveTemplate(sellerId, 3,MapUtil.hashMap("Description", wordStr+"seller模板描述"+wordStr));
		
		
		SaveCatalogPackageListCall saveCatalogPackageListCall = new SaveCatalogPackageListCall();
		int packageId1 = saveCatalogPackageListCall.callAndGetReturnData(MapUtil.hashMap("ProductPackageList[0]->SubCatalogName",wordStr+"测试组合001"+wordStr+"测试组合001"))
								.getJSONObject("Data").getJSONArray("SubCatalogIdList").getInt(0);
		
		
		int packageId2 = saveCatalogPackageListCall.callAndGetReturnData(MapUtil.hashMap("ProductPackageList[0]->SubCatalogName",wordStr+"测试组合002"+wordStr+"测试组合001"))
				.getJSONObject("Data").getJSONArray("SubCatalogIdList").getInt(0);
		
		
		//发布商品
		JSONObject addProductBean = JsonBeanHelper.getJsonBean("com.ymatou.iapi.sellerproduct/AddNewSellerProduct/AddNewProductTpl_Tc12.json");	
		Map<String, Object> updateMap = MapUtil.hashMap();
		updateMap.put("UserID", sellerId);
		updateMap.put("LoginID", sellerName);
		updateMap.put("Product->SizeTemplateId", sizeTemplate.getTemplateId());
		updateMap.put("Product->BuyerNoticeId", buyerNoticeTemplate.getTemplateId());
		updateMap.put("Product->SellerServiceId", sellerServiceTemplate.getTemplateId());
		updateMap.put("Product->iThirdCategoryId", 1123);
		updateMap.put("Product->Catalogs[0]->SubCatalogId", new JSONArray(Arrays.asList(packageId1)));
		updateMap.put("Product->Catalogs[0]->IsPackage", true);
		updateMap.put("Product->Catalogs[1]->SubCatalogId", new JSONArray(Arrays.asList(packageId2)));
		updateMap.put("Product->Catalogs[1]->IsPackage",true);
		
		updateMap.put("Product->ProductName", wordStr+"测试名称"+wordStr);
		updateMap.put("Product->ProductDescriptionInfo->WordContent", wordStr+"测试介绍"+wordStr);
		
		updateMap.put("Product->Catalogs[0]->PropertyValues[0]->PropertyValues[0]->PropertyValue", "测试属性值1"+wordStr+"测试"+wordStr);
		updateMap.put("Product->Catalogs[1]->PropertyValues[0]->PropertyValues[0]->PropertyValue", "测试属性值2"+wordStr+"测试"+wordStr);
		
		JsonBeanHelper.updateJsonBean(addProductBean, updateMap);

		//call&获取返回报文
		return SellerProductCallService.addNewProduct(addProductBean).getJSONObject("Data").getString("ProductID");
	}
	
	
}
